const Sequelize = require('sequelize')
const Op = Sequelize.Op
const sequelize = require('../db/dbconfig')
const ProjectLikeModel = require('../db/models/project_likes')
const ProjectInfoModel = require('../db/models/project_infos')
const projectLikeModel = ProjectLikeModel(sequelize)
const projectInfoModel = ProjectInfoModel(sequelize)

// 增
exports.addProjectLike = async (req, res) => {
  try {
    const project = req.params
    const user = req.auth
    let is_exist = await projectInfoModel.findOne({
      attributes: ['like_num'],
      where: {
        is_delete: 0,
        pro_id: project.id
      },
      raw: true
    })
    if(is_exist === null) return res.myMsg('点赞失败，项目不存在')
    let addLike = await projectInfoModel.update({
      like_num: is_exist.like_num+1
    },{
        where: {
      is_delete: 0,
          pro_id: project.id
    }
    })
    let results = await projectLikeModel.create({
      user_id: user.user_id,
      pro_id: project.id,
      raw:true
    })
    res.myMsg(results,0)
  } catch (e) {
    res.myMsg(e)
  }
}
// 删

// 改

// 查
